package test.yzx.pproject.act1;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import test.yzx.pproject.R;
import test.yzx.pproject.util.ListViewUtil;

public class ListViewUtilActivity extends AppCompatActivity {

    private ListView lv;
    private View topLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lv_util);
        lv = (ListView) findViewById(R.id.lv);
        topLayout = findViewById(R.id.topLayout);

        lv.post(new Runnable() {
            public void run() {

                View headerHolder = new View(ListViewUtilActivity.this);
                headerHolder.setLayoutParams(new ListView.LayoutParams(-1, 100*3));
                lv.addHeaderView(headerHolder, null, false);

                lv.setAdapter(new LvAdapter());
                new ListViewUtil().bindTargetItemScrollToHideRange(lv, 0, null, new ListViewUtil.RangeCallback() {
                    public void onChange(int length) {
                        topLayout.setTranslationY(-length/2);
                    }
                });

            }
        });
    }


    private class LvAdapter extends BaseAdapter{

        public LvAdapter(){
        }

        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        private List<Integer> hasAnimPositionList = new ArrayList<>(getCount());
        private boolean hasAnimated(int p){
            return hasAnimPositionList.contains(p);
        }
        private void setHasAnimated(int p){
            if(!hasAnimated(p))
                hasAnimPositionList.add(p);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = new TextView(ListViewUtilActivity.this);
                AbsListView.LayoutParams lp = new ListView.LayoutParams(-1, 100 * 3);
                convertView.setLayoutParams(lp);
                ((TextView)convertView).setText("XXXXXXXXXXXXXXXXX");
                convertView.setBackgroundColor(Color.RED);
            }

            final View cv = convertView;
            if(!hasAnimated(position)) {
                convertView.setTranslationY(100*3);
                convertView.setAlpha(0f);
                convertView.postDelayed(new Runnable() {
                    public void run() {
                   //     ViewAnimatorUtil.goBoundsTogether(cv, 400, 0, new DecelerateInterpolator(0.8f), null, 0f, null, null, 1f, null, null, null, null);
                    }
                },240);
            }
            setHasAnimated(position);
            return convertView;
        }
    }

}
